// @ts-ignore
/* eslint-disable */
import {request} from 'umi';
import {AssessmentListItem, AssessmentBody} from './data';
import {Key} from 'react';
import {EmployeeListItem} from '@/pages/Employee/List/data';

export async function dataList(
  params: {
    // query
    /** 当前的页码 */
    current?: number;
    /** 页面的容量 */
    pageSize?: number;
    category?: number;
  },
  sorter: any,
) {
  return request<{
    data: AssessmentListItem[];
    /** 列表的内容总数 */
    total?: number;
    success?: boolean;
  }>('/v2/enterprise/assessment/list', {
    method: 'GET',
    params: {
      ...params,
      pageNum: params.current,
      orderByColumn: Object.keys(sorter)[0],
      isAsc: Object.values(sorter)[0],
    },
  });
}

export async function loadData(param: AssessmentBody): Promise<{ data: AssessmentBody }> {
  return request(`/v2/enterprise/assessment/${param.assessmentId}`, {method: 'GET'});
}

export async function loadCheckData(param: AssessmentBody): Promise<{ data: AssessmentBody }> {
  return request(`/v2/enterprise/assessment/check/${param.assessmentId}`, {method: 'GET'});
}

export async function saveCheckData(param: { assessmentId: number, data: any }): Promise<{ data: number }> {
  return request(`/v2/enterprise/assessment/check/${param.assessmentId}`, {method: 'POST', data: param.data});
}

export async function addData(param: AssessmentBody): Promise<{ data: number }> {
  return request('/v2/enterprise/assessment', {method: 'POST', data: param});
}

export async function updateData(param: AssessmentBody): Promise<{ data: number }> {
  return request('/v2/enterprise/assessment', {method: 'PUT', data: param});
}

export async function updateCourse(param: AssessmentBody): Promise<{ data: number }> {
  return request('/v2/enterprise/assessment/course', {method: 'PUT', data: param});
}

export async function removeData(data: number[], options?: { [key: string]: any }) {
  return request<Record<string, any>>(`/v2/enterprise/assessment/` + data.join(), {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function removeQuestionData(
  dataId: number,
  data: number[],
  options?: { [key: string]: any },
) {
  return request<Record<string, any>>(`/v2/enterprise/assessment/${dataId}/` + data.join(), {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function loadSelectTag() {
  return request(`/v2/enterprise/assessment/tags`, {method: 'GET'});
}

export async function download() {
  request(`/v2/enterprise/assessment/question/template`, {
    method: 'POST',
    // 必须加responseType: 'blob',
    responseType: 'blob',
  }).then((res) => {
    const objectURL = URL.createObjectURL(res);
    let btn = document.createElement('a');
    btn.download = '试题模板.xlsx'; //文件类型
    btn.href = objectURL;
    btn.click();
    URL.revokeObjectURL(objectURL);
    // @ts-ignore
    btn = null;
  });
}

export async function questionList(dataId: number) {
  return request(`/v2/enterprise/assessment/question/${dataId}`, {
    method: 'GET',
  });
}

export async function coursewareListSave(dataId: number, ids: Key[]) {
  return request(`/v2/enterprise/assessment/courseware/save/${dataId}`, {
    data: ids,
    method: 'POST',
  });
}

export async function coursewareList(dataId: number) {
  return request(`/v2/enterprise/assessment/courseware/${dataId}`, {
    method: 'GET',
  });
}

export async function employeeListSave(dataId: number, ids: Key[]) {
  return request(`/v2/enterprise/assessment/employee/save/${dataId}`, {
    data: ids,
    method: 'POST',
  });
}

export async function employeeList(dataId: number) {
  return request(`/v2/enterprise/assessment/employee/${dataId}`, {
    method: 'GET',
  });
}

export async function employeeSelect(params: {
  // query
  /** 当前的页码 */
  current?: number;
  /** 页面的容量 */
  pageSize?: number;
  /** 部门ID */
  planId?: number;
}) {
  return request<{
    data: EmployeeListItem[];
    /** 列表的内容总数 */
    total?: number;
    success?: boolean;
  }>('/v2/enterprise/assessment/employee/select', {
    method: 'GET',
    params: {
      ...params,
      pageNum: params.current,
      // orderByColumn: "postSort",
      // isAsc: 'desc'
    },
  });
}

export async function publishData(data: number, options?: { [key: string]: any }) {
  return request<Record<string, any>>(`/v2/enterprise/assessment/publish/` + data, {
    data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function unPublishData(data: number, options?: { [key: string]: any }) {
  return request<Record<string, any>>(`/v2/enterprise/assessment/un-publish/` + data, {
    data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function loadAssessmentPostSelect(planId: number) {
  return request('/v2/enterprise/assessment/org/select?planId=' + planId, {method: 'GET'});
}
